/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * http//www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is OntologyIndexer.
 *
 * The Initial Developer of the Original Code is University of Roma Tor Vergata.
 * Portions created by University of Roma Tor Vergata are Copyright (C) 2006.
 * All Rights Reserved.
 *
 * OntologyIndexer was developed by the Artificial Intelligence Research Group
 * (ai-nlp.info.uniroma2.it) at the University of Roma Tor Vergata
 * Current information about OntologyIndexer can be obtained at 
 * http//ai-nlp.info.uniroma2.it/software/...
 *
 */

/*
 * Contributor(s): Armando Stellato stellato@info.uniroma2.it
 */

package it.uniroma2.art.owlart.langsearch;

import it.uniroma2.art.owlart.exceptions.ModelAccessException;
import it.uniroma2.art.owlart.langsearch.index.OntoTypeNotIndexableException;
import it.uniroma2.art.owlart.langsearch.index.config.IndexingConfiguration;
import it.uniroma2.art.owlart.langsearch.search.IndexAccessException;
import it.uniroma2.art.owlart.langsearch.search.OntIndexSearcher;
import it.uniroma2.art.owlart.model.ARTURIResource;
import it.uniroma2.art.owlart.models.SKOSModel;
import it.uniroma2.art.owlart.navigation.ARTURIResourceIterator;
import it.uniroma2.art.owlart.vocabulary.RDFResourceRolesEnum;
import it.uniroma2.art.owlart.vocabulary.SKOS;

import java.io.IOException;
import java.util.Collection;

import org.apache.lucene.queryParser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An extension of {@link OWLSearchEngine} for SKOS/SKOXL concept schemes.<br/>
 * It allows for the creation of a multilingual index associated to a given {@link SKOSModel}, for its
 * maintenance, and to query its content. <br/>
 * <br/>
 * <em>USAGE:</em><br/>
 * <ul>
 * <li>invoke the constructor, by specifying proper languages to be indexed; hint:
 * {@link #ConceptSchemeIndexer(SKOSModel, String, String...)} is the easiest to use and will cover most of
 * common cases</li>
 * <li>invoke either:</li>
 * <ul>
 * <li>{@link #buildIndex(boolean, VocabularyTypesEnum...)} (which is being overridden here to index skos
 * labels for SKOS concepts) or</li>
 * <li>build any number of custom indexes with
 * {@link #buildCustomIndex(String, ARTURIResourceIterator, boolean)} or
 * 
 * {@link #buildCustomIndex(String, ARTURIResourceIterator, ARTURIResource, boolean)} as from its super class</li>
 * </ul>
 * </ul> Note: not clear if and when {@link #initializeStandardIndexWriters(VocabularyTypesEnum...)} or
 * {@link #initializeCustomIndexWriters(Collection)} should be used<br/>
 * <br/>
 * 
 * @author Armando Stellato <stellato@info.uniroma2.it>
 */
public class SKOSSearchEngine extends OWLSearchEngine {

	public static enum skosAllTypes {
		prefLabel, altLabels, hiddenLabels
	};

	protected static Logger logger = LoggerFactory.getLogger(SKOSSearchEngine.class);

	protected SKOSModel skosModel;

	public SKOSSearchEngine(SKOSModel m, String IndexDir, IndexingConfiguration idxConfig) {
		super(m.getOWLModel(), IndexDir, idxConfig);
		skosModel = m;
	}

	/**
	 * builds an index of name "concept" containing all pairs of type &lt;concept, (value of skos:prefLabel
	 * property)&gt;<br/>
	 * 
	 * @param create
	 * @param conceptScheme
	 * @return
	 * @throws ModelAccessException
	 * @throws IOException
	 * @throws OntoTypeNotIndexableException
	 */
	public OntIndexSearcher buildConceptIndex(boolean create, ARTURIResource conceptScheme)
			throws ModelAccessException, IOException, OntoTypeNotIndexableException {

		ARTURIResourceIterator it = skosModel.listConceptsInScheme(conceptScheme);

		ontIndexManager.buildIndex(RDFResourceRolesEnum.concept.toString(), it, SKOS.Res.PREFLABEL, create);
		OntIndexSearcher searcher = new OntIndexSearcher(ontIndexManager, URILangs);
		ontSearcher = searcher;
		return searcher;
	}

	public OntIndexSearcher buildConceptIndex(boolean create) throws ModelAccessException, IOException,
			OntoTypeNotIndexableException {
		return buildConceptIndex(create, skosModel.getDefaultSchema());
	}

	public Collection<ARTURIResource> findConcepts(String searchString, String lang, int strategy)
			throws IndexAccessException, ParseException {
		logger.debug(ontSearcher + "  looking for concepts");
		return ontSearcher.findResources(m, RDFResourceRolesEnum.concept.toString(), searchString, lang,
				strategy);
	}

}
